package com.beinhospital_sw21_h2_05.beinhospitalsw21h205.controller;

import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.Department;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.DepartmentService;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.utils.PageBean;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 控制器类，处理与科室（Department）相关的HTTP请求
 */
@CrossOrigin
@Slf4j
@RestController
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 住院科室分页查询
     * @param page 页码，默认为1
     * @param pageSize 每页大小，默认为10
     * @param departmentName 科室名称，可选
     * @return 分页查询结果
     */
    @GetMapping("/department")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String departmentName) {
        log.info("住院科室分页查询：{}，{},{}", page, pageSize, departmentName);
        PageBean pageBean = departmentService.page(page, pageSize, departmentName);
        return Result.success(pageBean);
    }

    /**
     * 根据科室ID删除住院科室
     * @param departmentID 科室ID
     * @return 操作结果
     */
    @DeleteMapping("/department/{departmentID}")
    public Result delete(@PathVariable Integer departmentID) {
        log.info("根据id删除住院科室：{}", departmentID);
        departmentService.delete(departmentID);
        return Result.success();
    }

    /**
     * 新增科室
     * @param department 包含新增科室信息的对象
     * @return 操作结果
     */
    @PostMapping("/department")
    public Result insert(@RequestBody Department department) {
        log.info("新增科室：{}", department);
        departmentService.add(department);
        return Result.success();
    }

    /**
     * 编辑科室信息
     * @param department 包含编辑后的科室信息的对象
     * @return 操作结果
     */
    @PutMapping("/department")
    public Result update(@RequestBody Department department) {
        log.info("编辑科室：{}", department);
        departmentService.update(department);
        return Result.success();
    }
}
